package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.activity.service.IFlowService;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessFlag;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.FyProjectApply;
import com.ruoyi.system.domain.FyProjectApplyAncillary;
import com.ruoyi.system.domain.FyProjectPlan;
import com.ruoyi.system.domain.vo.business.*;
import com.ruoyi.system.dto.coopHan.PageCulvertNoticeDto;
import com.ruoyi.system.mapper.FyProjectApplyMapper;
import com.ruoyi.system.mapper.FyProjectPlanMapper;
import com.ruoyi.system.service.IFyProjectApplyAncillaryService;
import com.ruoyi.system.service.IFyProjectApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 剧本梗概备案申请Service业务层处理
 *
 * @author ruoyi
 * @date 2024-02-28
 */
@Service
public class fyProjectApplyServiceImpl extends ServiceImpl<FyProjectApplyMapper, FyProjectApply> implements IFyProjectApplyService {
    @Autowired
    private FyProjectApplyMapper fyProjectApplyMapper;
    @Autowired
    private IFyProjectApplyAncillaryService ancillaryService;
    @Autowired
    private IFlowService flowServiceImpl;
    @Autowired
    private FyProjectPlanMapper fyProjectPlanMapper;

    /**
     * 查询剧本梗概备案申请
     *
     * @param id 剧本梗概备案申请主键
     * @return 剧本梗概备案申请
     */
    @Override
    public FyProjectApply selectFyProjectApplyById(Long id) {
        return fyProjectApplyMapper.selectFyProjectApplyById(id);
    }

    /**
     * 查询剧本梗概备案申请列表
     *
     * @param fyProjectApply 剧本梗概备案申请
     * @return 剧本梗概备案申请
     */
    @Override
    public List<FyProjectApply> selectFyProjectApplyList(FyProjectApply fyProjectApply) {
        return fyProjectApplyMapper.selectFyProjectApplyList(fyProjectApply);
    }

    /**
     * 新增剧本梗概备案申请
     *
     * @param fyProjectApply 剧本梗概备案申请
     * @return 结果
     */
    @Override
    public int insertFyProjectApply(FyProjectApply fyProjectApply) {
        fyProjectApply.setCreateTime(DateUtils.getNowLocalDateTime());
        return fyProjectApplyMapper.insertFyProjectApply(fyProjectApply);
    }

    /**
     * 修改剧本梗概备案申请
     *
     * @param fyProjectApply 剧本梗概备案申请
     * @return 结果
     */
    @Override
    public int updateFyProjectApply(FyProjectApply fyProjectApply) {
        fyProjectApply.setUpdateTime(DateUtils.getNowLocalDateTime());
        return fyProjectApplyMapper.updateFyProjectApply(fyProjectApply);
    }

    /**
     * 批量删除剧本梗概备案申请
     *
     * @param ids 需要删除的剧本梗概备案申请主键
     * @return 结果
     */
    @Override
    public int deleteFyProjectApplyByIds(String ids) {
        return fyProjectApplyMapper.deleteFyProjectApplyByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除剧本备案申请信息
     *
     * @param id 剧本备案申请主键
     * @return 结果
     */
    @Override
    public int deleteFyProjectApplyById(Long id) {
        return fyProjectApplyMapper.deleteFyProjectApplyById(id);
    }

    @Override
    public List<FyProjectApplySelectListVO> list(FyProjectApplySelectListDto dto) {
        return fyProjectApplyMapper.list(dto);
    }

    @Override
    public FyProjectApplyDetailVO selectFyProjectApplyDetailVOById(Long id) {
        return fyProjectApplyMapper.selectFyProjectApplyDetailVOById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addOrUpdateSave(FyProjectApplySaveVO saveVO, Long id) throws Exception {
        if (StringUtils.isEmpty(saveVO.getFlowId())) {
            saveVO.setFlowId(null);
        }
        FyProjectApply entity = new FyProjectApply();
        BeanUtils.copyBeanProp(entity, saveVO);
        if (id == null || id == 0) {
            entity.setFlowId(null);
            entity.setId(null);
            this.save(entity);
            if ("0".equals(saveVO.getIsOutbox())) {
                // 非草稿箱
                flowServiceImpl.start(entity.getId(), BusinessFlag.FY_PROJECT.getValue());
            }
        } else {
            String isOutboxOld = this.getById(id).getIsOutbox();
            if ("1".equals(saveVO.getIsOutbox())) {
                entity.setFlowId(null);
            }
            this.updateById(entity);
            if ("1".equals(isOutboxOld) && "0".equals(saveVO.getIsOutbox())) {
                // 之前为草稿箱 保存为非草稿箱
                String processInstanceId = flowServiceImpl.start(entity.getId(), BusinessFlag.FY_PROJECT.getValue());
                entity.setFlowId(processInstanceId);
            }

        }

        FyProjectApplyAncillary ancillary = new FyProjectApplyAncillary();
        BeanUtils.copyBeanProp(ancillary, saveVO);
        ancillary.setFyProjectApplyId(entity.getId());
        if (id == null || id == 0) {
            ancillaryService.save(ancillary);
        } else {
            UpdateWrapper<FyProjectApplyAncillary> wrapper = new UpdateWrapper<>();
            wrapper.eq("fy_project_apply_id", ancillary.getFyProjectApplyId());
            ancillaryService.saveOrUpdate(ancillary, wrapper);
        }

    }

    @Override
    public Page<FyProjectApplySelectListVO> pageList(FyProjectApplySelectListDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setApprovalUserId(userId);
        Page<FyProjectApplySelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return fyProjectApplyMapper.getPageList(page, dto);
    }

    @Override
    public Page<FyProjectApplySelectListVO> pageNotice(PageCulvertNoticeDto dto) {
        Long userId = SecurityUtils.getUserId();
//        dto.setApprovalUserId(userId);
        Page<FyProjectApplySelectListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return fyProjectApplyMapper.getPageNotice(page, dto);
    }

    @Override
    public Page<FyProjectApplySelectManageListVO> pageManageList(FyProjectApplySelectManageListDto dto) {
        Page<FyProjectApplySelectManageListVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        Page<FyProjectApplySelectManageListVO> pageManageList = fyProjectApplyMapper.getPageManageList(page, dto);
        for (FyProjectApplySelectManageListVO vo : pageManageList.getRecords()) {
            LambdaQueryWrapper<FyProjectPlan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(FyProjectPlan::getFyProjectApplyId, vo.getId());
            wrapper.orderByDesc(FyProjectPlan::getCreateTime);

            FyProjectPlan fyProjectPlan = fyProjectPlanMapper.selectOne(wrapper.last("limit 1"));
            if (fyProjectPlan != null) {
                vo.setRemark(fyProjectPlan.getRemark());
            } else {
                vo.setRemark("暂无进度反馈");

            }
        }
        return pageManageList;
    }

    @Override
    public Page<FyProjectApply> allPassList(FyProjectApplyAllPassListDto dto) {
        Page<FyProjectApply> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        LambdaQueryWrapper<FyProjectApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(dto.getName()),FyProjectApply::getName,dto.getName());
        wrapper.like(StringUtils.isNotEmpty(dto.getUnit()),FyProjectApply::getUnit,dto.getUnit());
        wrapper.gt(StringUtils.isNotEmpty(dto.getStartTime()),FyProjectApply::getCreateTime,dto.getStartTime() +" 00:00:00");
        wrapper.le(StringUtils.isNotEmpty(dto.getEndTime()),FyProjectApply::getCreateTime,dto.getEndTime() +" 23:59:59");
        wrapper.eq(dto.getProjectType() != null && 0 != dto.getProjectType(),FyProjectApply::getType,dto.getProjectType());
        wrapper.eq(FyProjectApply::getStatus,1);
        wrapper.orderByDesc(FyProjectApply::getCreateTime);
        Page<FyProjectApply> fyProjectApplyPage = fyProjectApplyMapper.selectPage(page, wrapper);
        return fyProjectApplyPage;
    }
}
